/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package info.touret.winecellar.client;

import info.touret.winecellar.userinfo.client.UserInfoService;
import info.touret.winecellar.userinfo.client.UserInfoServiceAsync;
import info.touret.winecellar.userinfo.shared.UserInfoDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

/**
 * The top panel, which contains the 'welcome' message and various links.
 */
public class TopPanel extends Composite {

	interface Binder extends UiBinder<Widget, TopPanel> {
	}

	private static final Binder binder = GWT.create(Binder.class);

	@UiField(provided = true)
	HTML signOutLink;
	// @UiField
	// Anchor aboutLink;

	@UiField
	HTML welcomeTitle = new HTML();

	public TopPanel() {
		this.signOutLink = new HTML();
		initWidget(binder.createAndBindUi(this));
		createSignOutLink();
		createWelcomeTitle();
	}

	// @UiHandler("aboutLink")
	// public void onAboutClick(ClickEvent event){
	// AboutDialog dialog = new AboutDialog();
	// dialog.show();
	// }
	//

	private UserInfoServiceAsync userInfoService;

	public UserInfoServiceAsync getUserInfoService() {
		if (userInfoService == null) {
			setUserInfoService((UserInfoServiceAsync) GWT
					.create(UserInfoService.class));
		}
		return userInfoService;
	}

	public void setUserInfoService(UserInfoServiceAsync userInfoService) {
		this.userInfoService = userInfoService;
	}

	private void createWelcomeTitle() {
		AsyncCallback<UserInfoDTO> callback = new AsyncCallback<UserInfoDTO>() {
			@Override
			public void onFailure(Throwable e) {
				GWT.log("erreur dans la recuperation de l url de deconnexion:",
						e);
			}

			@Override
			public void onSuccess(UserInfoDTO title) {
				welcomeTitle.setHTML("Bienvenue " + title.getEmailAddress());
			}
		};
		getUserInfoService().getCurrentUserInfo(callback);
	}

	private void createSignOutLink() {
		final StringBuffer link = new StringBuffer();

		AsyncCallback<String> callback = new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable e) {
				GWT.log("erreur dans la recuperation de l url de deconnexion:",
						e);
			}

			@Override
			public void onSuccess(String url) {
				signOutLink.setHTML("<a href='" + url + "'>" + "Se déconnecter"
						+ "</a>");
				GWT.log(link.toString());
			}
		};
		getUserInfoService().createLogoutURL("/Winecellar.html", callback);

	}

	@Deprecated
	private void initializeURLS() {
		UserInfoServiceAsync userInfoService = (UserInfoServiceAsync) GWT
				.create(UserInfoService.class);

		AsyncCallback<String> callback = new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				GWT.log("erreur dans la recuperation de l url de deconnexion:"
						+ arg0.getLocalizedMessage());
			}

			@Override
			public void onSuccess(String arg0) {
				// signOutLink.setHTML(<a href="arg0);
			}
		};
		userInfoService.createLogoutURL("Se déconnecter", callback);
	}

}
